;;02/16/12 5:00 pm

;;CLOSE this file and UPDATE it BEFORE modifying it!
;;COMMIT changes *often* (and always update again before each commit)
;;workflow = update, make changes, update, commit

extensions [table]

;;human-types
breed [eggtraders eggtrader]    ;; egg traders
breed [livetraders livetrader]  ;; live poultry traders
breed [byowners byowner]        ;; backyard poultry owners (the owners move w/out poultry to other households, market, arena, etc)
                                ;;ai20120109: some byowners are fc owners
breed [farmowners farmowner]    ;; owners of large farms
breed [byduckowners byduckowner];; owners of small duck flocks/ backyard ducks
breed [fgdowners fgdowner]      ;; FGD owners (can move w/out ducks to households, egg cooperative, market, etc)
;;poultry-types
breed [fgds fgd]                ;; free-grazing duck flocks (FGD)
breed [bychickens bychicken]
breed [byducks byduck]
breed [farms farm]
;;inanimate-types
;breed [fgdfields fgdfield]      ;; FGD fields   ;;ai20110902 not used; are there plans to use this?
directed-link-breed [egg-links egg-link] ;ai20120101 why a *directed* link?
directed-link-breed [livebird-links livebird-link] ;ai20120101 why a *directed* link?
undirected-link-breed [newroads newroad]
breed [intersections intersection]


globals ;;ai: note that although NetLogo tempts one to use many globals, you shd avoid them when possible
[ 
  ;; CONSTANTS
  
  N-EGGTRADERS
  N-LIVETRADERS
  N-FGDOWNERS
  N-BYOWNERS
  N-FCOWNERS
  N-FARMOWNERS
  N-BYDUCKOWNERS
  N-BARNS
  N-PRACTICE-ARENAS
  N-MATCH-ARENAS
  N-MARKETS
  N-SLAUGHTERHOUSES
  N-COOPERATIVES
  day
  
  
  runsim?                  ;; boolean for simulation control
  stop-sim-reason          :: string for diagnostics
  
                          ;; patch agentsets
  rice-fields             ;; agentset containing the patches that are rice fields, which can be in 3 states
  roads                   ;; patch agentset containing the patches that are roads
  barns                   ;; patch agentset containing the patches that are FGD barns
  edge-patches            ;; patch agentset for the edge of the world
  agents        
  match-arenas
  practice-arena-1
  practice-arena-2
  markets
  slaughterhouses
  cooperatives
  human-types
  fc-owners
  humans
  poultry-types
  arenas
  trader-types

  TRANSPORT_WALK
  TRANSPORT_TRUCK
  
  ;; influenza toggle switch will control whether influenza transmission is included or not (on or off, respectvely)
;  field-infection-prob         ;; this is the probability that an exposed flock will be infected by a rice field
  latent-period          ;; this is the length of the latent period in days
  shedding-period-duck   ;; this is the length of the shedding period in days for ducks
  shedding-period-chicken;; this is the length of the shedding period in days for chickens
  road-contam-period     ;; time that the road will stay contaminated with viable virus
  trader-contam-period   ;; time that traders will stay contaminated after contact with infectious birds
  
  road-infect-prob       ;; probability that bkyd flock will be infected via infected FGD flocks passing on road
  egg-trader-contam-prob ;; probability that an egg trader will be contaminated when picking up eggs from an infected premeses
  live-trader-contam-prob ;; probabiltiy that a live trader will be contaminated when picking up birds from an infected premeses
  egg-trader-infect-prob ;; probability that a contaminated egg trader will cause infection of a susceptible flock he visits
  live-trader-infect-prob ;; probability that a contaminated live trader will cause infection of a susceptible flock he visits
  visit-infect-prob      ;; probability that a visitor that goes to an infected flock will infect its susceptible flock 
                         ;; *** does this hold true for an owner from an infected flock infecting a neighbor's flock? EMAIL EXPERTS to clarify.
  fc-infect-prob         ;; probability that a FC becomes infected while at the arena
  fc-bkyd-infect-prob    ;; probability that a FC infected at the arena will infect his home flock
  field-infection-prob
]

newroads-own
[
  roadtype 
  mypatches
]

patches-own
[
  rai                       ;;ai20110815
  land-type                 ;; see enumerations
  field-contaminant         ;; extent of contamination in [0,1] in rice field
                            ;; state 1: harvested: These are the fields that duck flocks move to 
                            ;; state 2: growing. Ducks do not graze in these fields 
                            ;; state 3: fallow. After ducks graze in the fields, they are left until the next harvest
  field-state               ;; "", "harvested", "growing", or "fallow"
  is-occupied?              ;; ab: is the field free of ducks?  ;;ai20110815 (consider ``is-target?`` for clarity)
  road-contam-time
  practice-arena-no
  barn?
  practice-arena-1?
  practice-arena-2?
  match-arena?
  market?
  cooperative?
  slaughterhouse?
  road-access
  pcontaminated?             ;; patches will be contaminated if next to a road with infectious ducks passing by; rice field can be contaminated
  infected-outsider-FC-here?
]


turtles-own                 ;;ai20110902 attributes common to all types
[
  infectious?               ;; is the agent infectious?
  can-contaminate?          ;; can this agent contaminate patches if infectious?
  home-patch                ;; location of barn for each FGD flock; household and yard with backyard poultry; household and yard with fighting cocks;
                            ;; household and slaughterhouse for local traders
  egg-pickup?               ;; does the turtle have eggs picked up?    
  egg-delivery?             ;; does the turtle deliver eggs to the market?    
  onsite-trader-no          ;; number of traders that come to flock to pick up eggs
  eggs-gone?                ;; eggs have been picked up for the day
  egg-type                  ;; This can be duck, chick or both egg types
  livebirds-gone?
  
]

fgds-own
[
  n-ducks
  RAI-PER-DUCK              ;;ai20110815 per duck daily consumption rate
  exposed?                  ;; exposed but not necessarily infected
  latent?                   ;; infected but not yet infectious
  recovered?                ;; no longer infectious
  transport-type            ;; ducks transported by walking or by truck
  target-field              ;; target is a harvested rice field (i.e., a dayfield)
  infection-time            ;; days since infection; Increases by one day with each tick.
  shedding-time             ;; shedding time; now is a discrete value but will be selected from prob dist (normal?)
  target-road-access
  field-only?
]

fgdowners-own
[
  
  pickup-days               ;; days that eggs are picked up by trader
  delivery-days
  links-in
  target-cooperative
  target-market
  contaminated?
  visit-list                ;; patch agentset
  ;; 12/14/11 these are included because to visit includes exposed. need to revamp the whole transmission aspect
  exposed?  
  latent?
  recovered?
  infection-time
  field-only?
  own-infected-flock?
]

bychickens-own
[
  exposed?                  ;; exposed but not necessarily infected
  latent?                   ;; infected but not yet infectious
  recovered?                ;; no longer infectious
  infection-time            ;; days since infection; Increases by one day with each tick.
  shedding-time             ;; shedding time; now is a discrete value but will be selected from prob dist (normal?)
]

byowners-own
[                         
  contaminated?
  target-visit
  visit-list     
  own-fightingcocks?
  target-practice-arena
  target-match-arena
  links-in            
  pickup-days
  delivery-days
  practice-fight-week?
  match-fight-week?
  own-infected-flock?
  latent?
  own-infected-fc?
]

farms-own
[
  exposed?                  ;; exposed but not necessarily infected
  latent?                   ;; infected but not yet infectious
  recovered?                ;; no longer infectious
  infection-time            ;; days since infection; Increases by one day with each tick.
  shedding-time             ;; shedding time; now is a discrete value but will be selected from prob dist (normal?)

]

farmowners-own
[                          
  contaminated?                
  target-visit
  visit-list     
  links-in 
  pickup-days
  delivery-days
  target-cooperative
  target-market
  own-infected-flock?
]

byducks-own
[
  exposed?                  ;; exposed but not necessarily infected
  latent?                   ;; infected but not yet infectious
  recovered?                ;; no longer infectious
  infection-time            ;; days since infection; Increases by one day with each tick.
  shedding-time             ;; shedding time; now is a discrete value but will be selected from prob dist (normal?)
]

byduckowners-own
[  
  contaminated?                         
  target-visit
  visit-list    
  links-in 
  pickup-days
  delivery-days
  target-market
  own-infected-flock?
]

eggtraders-own
[
  contaminated?
  target-eggfarm
  collecting?
  target-market
  eggtrader-links
  trader-contam-time
]


livetraders-own
[
  target-livefarm
  contaminated?
  collectingbirds?
  target-slaughterhouse
  livetrader-links
  bird-pickup-days
  target-livebirds
  trader-contam-time
]

intersections-own
[
  name
  int_type
]


;;;;;;;;;;;;;;;;;;;;;;;;
;;     UTILITIES      ;;
;;;;;;;;;;;;;;;;;;;;;;;;

;;ai: return a single draw from a binomial distribution
to-report binomial [n p] ;; written by Seth Tisue
  report length filter [? < p] n-values n [random-float 1]
end

;; return a result (value) based on the defined probabilities (#weights) of each value
;; written by Nick Bennet 2009 http://groups.yahoo.com/group/netlogo-users/message/9091 
to-report random-weighted [#values #weights]
  let selector (random-float sum #weights)
  let running-sum 0
    (foreach #values #weights [
      set running-sum (running-sum + ?2)
      if (running-sum > selector) [
        report ?1
      ]
    ])
end

;;ai20110901: a logging utility
to log2logfile [#msg]
  file-open "birdflu-xxx.log"
  file-print #msg
  file-close
end

;;ai: ``split`` was written by Jim Lyons (2007)
to-report split [ #string #sep ] ; #sep must be non-empty string
  let result [] ; return value
  let w length #sep
  loop ; exit when done
  [ let next-pos position #sep #string
  if not is-number? next-pos
  [ report reverse (fput #string result) ]
  set result fput (substring #string 0 next-pos) result
  set #string substring #string (next-pos + w) (length #string)
  ]
end

;;ai: use law of sines to report determine
;;    minimum distance from patch to link (no wrapping)
to-report patch-near-link? [#patch #link]
  let max-distance 1
  let eps 0.0001
  set max-distance 1 + eps
  let dist 0
  let angles (list )
  let result true
  ask #link
    [
      ask both-ends
      [
        if (patch-here != #patch)
        [
          let angle abs (subtract-headings towards #patch towards other-end)
          if-else (angle > 90)
          [set result false]
          [
            set dist (sin angle) * distance #patch
            if (dist > max-distance) [set result false]
          ]
        ]
       ]
     ]
  report result
end

to-report random-pert [#minval #likeval #maxval]
  ;use pert params to draw from a beta distribution
  let pert-var 1. / 36
  let pert-mean (#maxval + 4 * #likeval - 5 * #minval) / (6 * (#maxval - #minval))
  let temp pert-mean * (1 - pert-mean) / pert-var
  let alpha1 pert-mean * (temp - 1)
  let alpha2 (1 - pert-mean) * (temp - 1)
  let x1 random-gamma alpha1 1
  let x2 random-gamma alpha2 1
  report x1 / (x1 + x2)
end

;;;;;;;;;;;;;;;;;;;;;;;;
;;  SETUP PROCEDURES  ;;
;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;
;;Globals;;
;;;;;;;;;;;

to startup  ;;ai: this procedure will run automatically when the model loads
  set-default-shape byowners "person"
  set-default-shape eggtraders "car"
  set-default-shape fgds "duck"
  set-default-shape fgdowners "person"
  set-default-shape livetraders "person"
  set-default-shape farmowners "person"
  set-default-shape byduckowners "person"
  set-default-shape livetraders "truck"
  set-default-shape farms "farm"
  set-default-shape byducks "bird side"
  set-default-shape bychickens "backyard"
end


to setup                   ;; setup the display with globals, patches, agents
  clear-all
  set human-types (list eggtraders livetraders byowners farmowners byduckowners fgdowners fc-owners)  ;;ai: this needs to be a complete list! chk
  set humans turtles with [member? breed human-types]
  set poultry-types (list fgds bychickens byducks farms)  ;;ai: this needs to be a complete list! chk
  set trader-types (list eggtraders livetraders)
  
  ;;ai20110901 create a file for event logging
  ;;please do not delete unless this is causing problems for you!
  carefully [file-delete "birdflu-xxx.log"] []
  file-open "birdflu-xxx.log"
  file-print date-and-time
  file-print ""
  file-close
  
  set runsim? true          ;; set runsim? false for programmatic stop of simulation
  setup-globals
  setup-newroads
  setup-patches
  setup-agents
end

;; Setup the enumerations
to setup-globals
  
;  set TRANSPORT_WALK 1
;  set TRANSPORT_TRUCK 2
  
  ;;ai: set values of constants      ;; rai/d/d based on data from 151 flock owner surveys
                                     ;; generated Pert distribution, then prob histogram for values
                                     ;; normal distribution added too much to extremes (1 and 7) after truncation
  set N-EGGTRADERS 6                                  
  set N-LIVETRADERS 4                                 
  set N-FGDOWNERS 9
  set N-BYOWNERS 250
  set N-FCOWNERS 50
  set N-FARMOWNERS 6
  set N-BYDUCKOWNERS 80
  set N-BARNS 360
  set N-MATCH-ARENAS 1
  set N-MARKETS 3
  set N-COOPERATIVES 1
  set N-SLAUGHTERHOUSES 3
  
  set day 1                                  ;; model starts running on day 1, which is Monday.
  
  set field-infection-prob 100                     ;; setup the new state change parameters
  set latent-period 2
  set shedding-period-chicken 4
  set shedding-period-duck 7
  set road-contam-period 1
  set trader-contam-period 1
  
  set road-infect-prob 55      
  set egg-trader-contam-prob 70
  set live-trader-contam-prob 83
  set egg-trader-infect-prob 70
  set live-trader-infect-prob 82
  set visit-infect-prob 43     
  set fc-infect-prob 48        
  set fc-bkyd-infect-prob 83   
  
end

;;;;;;;;;
;Patches;
;;;;;;;;;

to setup-newroads
  let nodes table:make  ;;
  file-open "intersections.txt"
  while [not file-at-end?]
  [
    let data file-read-line
    if not (first data = "#")
    [
      set data split data " "
      ;; cast the co-ordinates from strings to numbers
      let x read-from-string item 0 data
      let y read-from-string item 1 data
      ;; get the intersection name and intersection type
      let iname item 2 data
      let itype last data
      let ilocation list (item 0 data) (item 1 data)
      ask patch x y [
        sprout-intersections 1 [
          setxy x y
          table:put nodes iname self  ;;map name to intersection (node)
          set color black
          set shape "target"
          set size 0.5
          set label (word who "(" iname ")")
        ]
      ]
      ask intersections [set home-patch patch-here ]
    ]
  ]
  file-close
  print nodes
  file-open "roads.txt"
  while [not file-at-end?]
  [
    let data file-read-line
    if not (first data = "#")   ;; prevent reading of the first line
    [
      set data split data " "   ;; split location indicated by a space
      let node1 table:get nodes item 0 data
      let node2 table:get nodes item 1 data
      ask node1 [create-newroad-with node2 
        [
          set roadtype last data 
        ]]
      ask newroads
        [
          if roadtype = "highway" [set color 33 set thickness 0.5]
          if roadtype = "major_road" [set color 34 set thickness 0.5]
          if roadtype = "loose_road" [set color 35 set thickness 0.5]
          if roadtype = "small_road" [set color 36 set thickness 0.5]
          if roadtype = "field_road" [set color 37 set thickness 0.25]
        ]
    ]
  ]
  file-close
  print "roads: "
  print newroads
  print [roadtype] of newroads
  ;;ai:20120118
  ask newroads [set mypatches patches with [patch-near-link? self myself]] ;;ai: every road keeps a set of neaby patches
  ask newroads with [roadtype = "major_road"] [ask mypatches [set pcolor red]]
  ask newroads with [roadtype = "loose_road"] [ask mypatches [set pcolor violet]]
  ask newroads with [roadtype = "small_road"] [ask mypatches [set pcolor pink]]
end

to setup-patches
  
  ask patches                     ;; initialization: all patches are growing rice fields
  [
    set land-type "rice"
    set field-state "growing"
    set is-occupied? false  ;;ai20110815
    set road-access min-one-of ( intersections )[ distance myself ]  ;;ai: min-n-of produces an agentset!
    set pcontaminated? FALSE
  ]
  
  setup-edge-patches
; make-roads           ;;build roads **and edge patches** and set field-state ""
  
    ask n-of 1 newroads with [roadtype = "loose_road" ]
  [
    ask n-of 1 mypatches with [not any? turtles-here]
      [ 
        set practice-arena-1? TRUE
        set field-state ""
        set infected-outsider-FC-here? FALSE
      ]
  ]
  ask n-of 1 newroads with [roadtype = "loose_road"]
  [
    ask n-of 1 mypatches with [(not any? turtles-here)]
      [ 
        set practice-arena-2? TRUE
        set field-state ""
        set infected-outsider-FC-here? FALSE
      ]
  ]
  ask n-of 1 newroads with [roadtype = "loose_road"]
  [
      ask n-of 1 mypatches with [(not any? turtles-here)]
      [ 
        set match-arena? TRUE
        set field-state ""
        set infected-outsider-FC-here? FALSE
      ]
  ]
  ask n-of N-SLAUGHTERHOUSES newroads with [roadtype = "loose_road"]
  [
       ask n-of 1 mypatches with [(not any? turtles-here)]
      [ 
        set slaughterhouse? TRUE
        set field-state ""
      ]
  ]
  set practice-arena-1 patches with [ practice-arena-1? = TRUE ]
  set practice-arena-2 patches with [ practice-arena-2? = TRUE ]
  set match-arenas patches with [ match-arena? = TRUE ]
  set slaughterhouses patches with [slaughterhouse? = TRUE]
 
 ask n-of N-MARKETS newroads with [roadtype = "major_road"]
  [
    ask n-of 1 mypatches with [(not any? turtles-here)]
      [ 
        set market? TRUE
        set field-state ""
      ]
  ]
  ask n-of N-COOPERATIVES newroads with [roadtype = "major_road"]
  [
    ask n-of 1 mypatches with [(not any? turtles-here)]
      [ 
        set cooperative? TRUE
        set field-state ""
      ]
  ]
  set markets patches with [ market? = TRUE ]
  set cooperatives patches with [ cooperative? = TRUE ]
  
  
  ;; create barns
  ;; distribute barns to  road patches, having 60%, 30% and 10% around major, loose and small roads
  ;; old proportions: [["major_road" 0.3063] ["loose_road" 0.1465] ["small_road" 0.1921]]
  foreach [["major_road" 0.6] ["loose_road" 0.3] ["small_road" 0.1]]
  [
    let my-rtype first ?
    let my-roads newroads with [roadtype = my-rtype]
    let my-potential-barns patch-set [mypatches with [not any? turtles-here and field-state = "growing"]] of my-roads 
    print (word count my-potential-barns " potential barns along " my-rtype)
    let my-nbarns round (last ? * n-barns)
    print (word "create " my-nbarns " barns along " my-rtype)
    ask n-of my-nbarns my-potential-barns [set barn? true set field-state ""]
  ]
  set barns patches with [ barn? = TRUE ]
  print (word (count barns) " barns created")

;;  ask newroads with [roadtype = "major_road"]       ;; have to use just patches without turtles becuase otherwise we will include intersection patches
;;  [
;;      ask n-of (count ( mypatches with [not any? turtles-here]) * 0.3063) mypatches with [ (field-state = "growing") and (not any? turtles-here) ]
;;      [ 
;;         set barn? TRUE
;;         set field-state ""
;;      ]
;;    ]
;;   ask newroads with [roadtype = "loose_road"]
;;  [
;;      ask n-of (count mypatches with [not any? turtles-here] * 0.1465) mypatches with [ (field-state = "growing") and (not any? turtles-here) ]
;;      [ 
;;         set barn? TRUE
;;         set field-state ""
;;      ]
;;    ]
;;   ask newroads with [roadtype = "small_road"]
;;  [
;;      ask n-of (count mypatches with [not any? turtles-here] * 0.1921) mypatches with [ (field-state = "growing") and (not any? turtles-here) ]
;;      [ 
;;         set barn? TRUE
;;         set field-state ""
;;      ]
;;    ]
;;  set barns patches with [ barn? = TRUE ]
;;  ask barns [ set field-state "" ]  
  
  ;;ai: now set up all the fields and field-states
  set rice-fields (patches with [ field-state = "growing" ])
  ;; make random growing fields into harvested fields
  ask n-of num-fields-harvested rice-fields with [ (field-state = "growing") ]
  [ 
    harvest                         ;;ai: changes field-state (and color)
  ]
  
  ;; make number of harvested fields determined by slider to be contaminated
  if influenza?
  [ 
    ask n-of num-fields-contam rice-fields with [ field-state = "harvested" ]
    [                                                             
      contaminate-field                     ;;ai20110815 (just matches what you have been doing, but perhaps shd change)
    ]
  ]
  ask n-of 10 rice-fields with [ field-state = "growing" ]  ;; make random growing fields into fallow fields
  [
    set field-state "fallow"
  ]
  
  ask patches [ set-patch-color ]
end

to setup-edge-patches
  
  set edge-patches patches with [count neighbors != 8]               ;; make all edge patches growing fields so that turtles don't hit wall
  ask edge-patches
  [
    set land-type ""                            ;; learned from the Look Ahead Example Model in Library
    set field-state ""
  ]
end

to harvest ;;rice field proc
  if not (field-state = "growing") [ __error "wrong state" ]
  set field-state "harvested"
  let alt-vals n-values 29 [(? + 1) * 20] ;;ai: use these vals instead?
  set rai random-weighted [ 20 30 50 70 90 110 130 150 170 190 210 230 250 270 290 310 330
    350 370 390 410 430 450 470 490 510 530 550 570 ]
  [ 0.0003 0.0181 0.0367 0.0504 0.0642 0.0647 0.0762 0.0778 0.073 0.0691 0.0636 0.0551 0.0524 0.0536 
    0.0487 0.0404 0.0347 0.0266 0.0233 0.0189 0.0171 0.012 0.009 0.0067 0.0038 0.0022 0.0011 0.0002 0.0001 ]

  set-patch-color
end

to set-patch-color
  ifelse (field-state != "")
  [
    if (field-state = "fallow") [ set pcolor 48 ]
    if (field-state = "harvested") [ set pcolor yellow ]
    if (field-state = "growing") [ set pcolor 67 ]  ;;light green
  ]
  [
    if (member? self match-arenas) [ set pcolor red set plabel "A"]
    if (member? self (patch-set practice-arena-1 practice-arena-2)) [ set pcolor red set plabel "P" ]
    if (member? self barns) [ set pcolor (blue + 1) ]
    if (member? self markets) [ set pcolor violet set plabel "M"]
    if (member? self cooperatives) [ set pcolor violet set plabel "C"]
    if (member? self slaughterhouses) [ set pcolor 13 set plabel "S" ]
    if (member? self edge-patches) [ set pcolor white ]
  ]
end



;;;;;;;;;;;;
;; Agents ;;
;;;;;;;;;;;;

to setup-agents
 ; let humans turtles with [member? breed human-types]
  
  ;; LIVETRADERS: sprout livetraders
  ask one-of barns  with [ (not any? turtles-here ) and (pxcor > 0 ) and (pycor > 0 ) ]
  [
    sprout-livetraders 1 [ set home-patch patch-here init-livetrader ]
  ]
  ask one-of barns  with [ (not any? turtles-here ) and (pxcor > 0 ) and (pycor < 0 ) ]
  [
    sprout-livetraders 1 [ set home-patch patch-here init-livetrader ]
  ]
  ask one-of barns  with [ (not any? turtles-here ) and (pxcor < 0 ) and (pycor > 0 ) ]
  [
    sprout-livetraders 1 [ set home-patch patch-here init-livetrader ]
  ]
  ask one-of barns  with [ (not any? turtles-here ) and (pxcor < 0 ) and (pycor < 0 ) ]
  [
    sprout-livetraders 1 [ set home-patch patch-here init-livetrader ]
  ]
  
  ;;FGD: sprout num-fgd FGD flocks from barn (home patch) patches
  ask n-of num-fgd-barntofield barns with [ not any? turtles-here ]
  [
    sprout-fgds 1 [ set home-patch patch-here init-fgd set field-only? FALSE ]
    sprout-fgdowners 1 [set home-patch patch-here init-fgdowner set field-only? FALSE ]
  ]
   ask n-of num-fgd-fieldonly rice-fields with [ (field-state = "harvested") and (not any? turtles-here )]
  [
    sprout-fgds 1 [ set home-patch patch-here init-fgd set field-only? TRUE ]
  
    sprout-fgdowners 1 [ set home-patch patch-here init-fgdowner set field-only? TRUE ]

  ]
    ask n-of num-fgd-inf fgds [infect-fgd]         ;; set some of the fgd flocks as infectious
  
  ;;BYOWNERS: sprout backyard poultry owners from barn patches, assign a certain number to have fighting cocks
  ask n-of N-BYOWNERS barns  with [ not any? turtles-here ]
  [
    sprout-byowners 1 [ set home-patch patch-here init-byowner ]        
    sprout-bychickens 1 [ init-bychickens ] 
  ]
  
  
  ;ai20120110: fc-owners membership won't change
  set fc-owners n-of N-FCOWNERS byowners
  ask fc-owners [ set own-fightingcocks? true ]
  
  ;;BYDUCKOWNERS: sprout backyard duck owners from barn patches          
  ask n-of N-BYDUCKOWNERS barns with [ not any? turtles-here ]
  [
    sprout-byduckowners 1 [ set home-patch patch-here init-byduckowner ]
    sprout-byducks 1 [ init-byducks ] 
  ]
  
  ;;FARMOWNERS: sprout farm owners from barn patches                       
  ask n-of N-FARMOWNERS barns with [ not any? turtles-here ]
  [
    sprout-farmowners 1 [ set home-patch patch-here init-farmowner ]
    sprout-farms 1 [ init-farms ] 
  ]
  

  ;;;; Establish visit-lists for all poultry owners
  ;;ai20120110: (but left it as you had it: no visit-list for eggtraders and livetraders)
  ;;  comment: this does not ensure the neighbor will be home when visited! 
  ;;  ab response: This is ok, we are more concerned about the feces on the ground that the visitor might step in
 ; let humans turtles with [member? breed human-types]
  file-open "birdflu-xxx.log"  ;;log info for debugging
  foreach (list byowners fgdowners byduckowners farmowners)
  [
    ask ?
    [
      let my-home-patch home-patch
      ;; visit-list is a patch-set
      set visit-list min-n-of ( binomial 7 0.42857) (patch-set [home-patch] of other turtles with [ member? breed human-types] ) [distance my-home-patch]
      ;; the next 2 lines just log for debugging
      file-show (word "turtle " who " with type " breed " has visit list: " [self] of visit-list)
      ask visit-list [file-print (word "\tdistance: " (distance myself))]
    ]
  ]

  file-close
  
  set-egg-types
  set-egg-pickups
  set-egg-deliveries
  make-egg-pickup-network
  set-eggtrader-type
  setup-fighting-cocks
  print "set up fighting cocks"
  
end


;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Agent Initializations ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;

to init-byowner
  set color (orange + 2)
  set size 1 
  set can-contaminate? FALSE
  set egg-type ""
  set egg-pickup? FALSE
  set egg-delivery? FALSE
  set eggs-gone? FALSE
  set own-fightingcocks? FALSE
  set livebirds-gone? FALSE
  set practice-fight-week? FALSE
  set match-fight-week? FALSE
  set onsite-trader-no 0
  set own-infected-flock? FALSE
  set contaminated? FALSE
  set latent? False
  set infectious? FALSE
end

to init-fgdowner
  set color white
  set size 1 
  set can-contaminate? FALSE
  set egg-type "duck"
  set egg-pickup? FALSE
  set egg-delivery? FALSE
  set eggs-gone? FALSE
  set contaminated? FALSE
  set infectious? FALSE                 ;; The number of traders per FGD flock will vary, as determined by a PD
  set onsite-trader-no random-weighted [ 1 2 3 4 ] [ 0.513 0.38 0.099 0.0078 ]   
  print (word "flock " who " has " onsite-trader-no " onsite traders")                
  set livebirds-gone? FALSE
  
end

to init-byduckowner
  set color (brown + 2)
  set can-contaminate? FALSE
  set egg-type "duck"
  set egg-pickup? FALSE                  ;; 28 will be meat only, 41 will be egg only, 11 will be egg and meat
  set egg-delivery? FALSE                ;; of those that will have eggs (52 flocks), half will pick up and half will deliver
  set eggs-gone? FALSE
  set onsite-trader-no 1                 ;; The number of traders per small duck flock will stay 1
  set livebirds-gone? FALSE
  set own-infected-flock? FALSE
  set contaminated? FALSE
end

to init-farmowner
  set color (yellow + 2)
  set can-contaminate? FALSE
  set egg-type ""
  set egg-pickup? FALSE
  set egg-delivery? FALSE
  set eggs-gone? FALSE                   ;; The number of traders per farm will vary, as determined by a PD
  set onsite-trader-no random-weighted [ 1 2 3 4 5 6 7 8 ] [ 0.2216 0.3602 0.2156 0.1171 0.0548 0.0223 0.0071 0.0013 ]   
  print (word "farm " who " has " onsite-trader-no " onsite traders")  
  set livebirds-gone? FALSE
  set own-infected-flock? FALSE
  set contaminated? FALSE
end

to init-bychickens
  set color orange
  set infectious? FALSE 
  set exposed? FALSE
  set latent? FALSE
  set recovered? FALSE
end

to init-byducks
  set color brown
  set infectious? false
  set exposed? FALSE
  set latent? False
  set recovered? FAlse
end

to init-farms
  set color yellow
  set infectious? FALSE
  set exposed? FALSE
  set latent? FALSE
  set recovered? FALSE
end

to init-fgd  ;;initializations for an fgd
  let rw-values n-values 23 [(? + 1) * 1e-3]
  let rw-weights [ 0.0037 0.0383 0.0728 0.0846 0.0844 0.0796 0.0814 0.0794 0.0715 0.0645 0.0605 0.0534 0.0479 0.0393 0.0303 0.027 0.0222 0.0178 0.015 0.01 0.0069 0.0051 0.0044 ]
  set RAI-PER-DUCK random-weighted rw-values rw-weights
  print (word "rai-per-duck is " rai-per-duck)
  set n-ducks 2000   ;;ai20110815
  set infectious? FALSE
  set exposed? FALSE
  set latent? FALSE
  set recovered? FALSE
  set heading 270
  set color white
  set size 2
  set target-field nobody  ;;ai20110815
  set can-contaminate? TRUE
  set egg-type ""
  set eggs-gone? FALSE
  set egg-pickup? FALSE
end

to init-eggtrader
  set color (pink + 1)
  set size 1  
  set can-contaminate? FALSE         ;; traders cannot contaminate, becuase i do not want them to contaminate the roads
  set contaminated? FALSE            ;; traders may contaminate the target-farm, so this is a separate command
  set eggtrader-links random-weighted [ 1 2 3 4 5 6 7 8 9 ] [ 0.0929 0.286 0.247 0.1783 0.1115 0.0537 0.0248 0.0052 0.0006 ]   
 ; print (word "Egg trader " who " services " eggtrader-links " farms")               
  set egg-type ""
end

to init-livetrader
  set color (magenta + 1)
  set size 1  
  set label-color (blue - 2)
  set can-contaminate? FALSE
  set egg-type ""
  set contaminated? FALSE
  set egg-pickup? FALSE
  set bird-pickup-days random 3      ;; Each live trader will be assigned a random number (0, 1, 2), which will indicate the number of trading days (1, 2, 3)
  print (word "livetrader " who " has " bird-pickup-days " bird-pickup-days.")
end

;;;;;;;;;;;;;;;;;
;; Egg Network ;;
;;;;;;;;;;;;;;;;;

to set-egg-types
 
  ask n-of 5 fgdowners [set egg-pickup? TRUE ]
  ask n-of 4 fgdowners [set egg-delivery? TRUE ]
  
  ask n-of 26 byduckowners [set egg-pickup? TRUE ]         ;; of those byduck flocks that will have eggs (52 flocks), half will pick up and half will deliver
  ask n-of 26 byduckowners [set egg-delivery? TRUE ]
  
  ask n-of 3 farmowners with [ egg-type = ""] [ set egg-type "chicken" ]
  ask farmowners with [ egg-type = ""] [ set egg-type "duck" ]
  ask n-of 2 farmowners with [egg-type = "chicken" ] [set egg-pickup? TRUE ]
  ask n-of 2 farmowners with [egg-type = "duck" ] [set egg-pickup? TRUE ]
  ask n-of 2 farmowners with [egg-pickup? = FALSE] [set egg-delivery? TRUE ]
  ask one-of farmowners with [ (egg-type = "chicken") and (egg-pickup? = TRUE) ] [set egg-delivery? TRUE ]
  ask one-of farmowners with [ (egg-type = "duck") and (egg-pickup? = TRUE) ] [set egg-delivery? TRUE ]
  
  
  ask farms with [any? farmowners-here with [egg-type = "chicken"]] [ set egg-type "chicken"]
  ask farms with [any? farmowners-here with [egg-type = "duck"]] [ set egg-type "duck"]
  
end

to set-egg-pickups
  ask farmowners with [egg-pickup? ] 
  [
    set pickup-days random-weighted [ 2 3 7 ] [ 0.33 0.33 0.33 ]
  ] ;; The number of pickup days will vary for each egg supplier        
  ask fgdowners with [egg-pickup? ] 
  [
    set pickup-days random-weighted [ 1 2 3 7 ] [ 0.073 0.635 0.197 0.095 ]
  ] ;; based on large survey data
  ask byduckowners with [egg-pickup? ] 
  [
    set pickup-days random-weighted [ 1 2 3 7 ] [ 0.073 0.635 0.197 0.095 ]
  ] ;; same distribution as FGD
    
end

to set-egg-deliveries
  ask farmowners with [egg-delivery? ]                                         ;; The number of delivery days will vary for each supplier
  [
    set delivery-days random-weighted [ 2 3 7 ] [ 0.33 0.33 0.33 ]    
  ]
  ask fgdowners with [egg-delivery? ] 
  [
    set delivery-days random-weighted [ 1 2 3 7 ] [ 0.087 0.522 0.174 0.217 ]
  ] ;; based on large survey data
  ask byduckowners with [egg-delivery? ] 
  [
    set delivery-days random-weighted [ 1 2 3 7 ] [ 0.087 0.522 0.174 0.217 ]
  ] ;; same distribution as FGD
end

;;Goals for ``make-egg-network``:
;;1. set the number of egg-traders "needed" by each turtle (i.e., onsite-trader-no)
;;2. set the number of sites each eggtrader can visit to 2 now, later randomly (i.e., eggtrader-links)
;;3. create just as many eggtraders as are needed
;;4. create eggtraders fairly close to their suppliers
;;5. associate each eggtrader with a barn, where a barn is associated with 0 or 1 eggtraders
to make-egg-pickup-network
  let egg-suppliers turtles with [ egg-pickup? = True ]
  ;start with every barn having a potential egg-trader
  ask barns [sprout-eggtraders 1 [ set home-patch patch-here init-eggtrader ] ] 
  
  ;ai20120101 each egg trader makes links to suppliers (until suppliers all taken)
  ask eggtraders
  [
    carefully
    [
      create-egg-links-from min-n-of eggtrader-links
      egg-suppliers with [ count my-out-links < onsite-trader-no ]
      [distance myself]
    ]
    [
      die
    ]
  ]
  ask egg-links [ hide-link ]
end

to set-eggtrader-type
  ask eggtraders
  [
    ifelse all? in-link-neighbors [egg-type = "chicken"]
    [ set egg-type "chicken"] 
    [
      ifelse all? in-link-neighbors [egg-type = "duck" ]
      [set egg-type "duck"]
      [
        if any? (in-link-neighbors with [egg-type = "duck"]) and any? (in-link-neighbors with [egg-type = "chicken"])
        [set egg-type "duck and chicken"]
      ]
    ]
  ]
end
      
;;;;;;;;;;;;;;;;;;
;;Fighting Cocks;;
;;;;;;;;;;;;;;;;;;     
      
to setup-fighting-cocks
  let practice-arenas patches with [plabel = "P"]
  ask fc-owners
    [
      set target-practice-arena min-one-of practice-arenas [ distance myself ] 
      ;print (word "FC " who " chose arena" ([pxcor] of target-practice-arena) ([pycor] of target-practice-arena))
    ]
  ask fc-owners [ if [pycor] of target-practice-arena > 0 [ if random-float 100 < 50 [ set practice-fight-week? true ]]]  ;; about half of those at each practice arena bring cocks to fight
  ask fc-owners [ if [pycor] of target-practice-arena < 0 [ if random-float 100 < 50 [ set practice-fight-week? true ]]]
  ask fc-owners
    [
      set target-match-arena min-one-of match-arenas [ distance myself ] 
      ;print (word "FC " who " chose arena" ([pxcor] of target-match-arena) ([pycor] of target-match-arena))
    ]
end

;;;;;;;;;;;;;;;;;
;;Go procedures;;
;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;
;;Grazing Procs;;
;;;;;;;;;;;;;;;;;

to go-to-field
  ask fgds [ set-target-field ]             ;;ai: also sets is-occupied? on target fields!
  ask fgds [travel-roads-to target-field]
  ask fgds [ graze-here ]                    ;; infection possible during grazing
end  

to set-target-field ;;ai20110815 fgd proc
  let rai-needed (n-ducks * RAI-PER-DUCK)
  print (word "rai-needed " rai-needed)
  let change-target? false
  let old-target target-field
  if ((old-target = nobody) or ([rai] of old-target < rai-needed)) ;; then change target
  [
    let candidates rice-fields with [ (field-state = "harvested") and (rai >= rai-needed) and (not is-occupied?)]
    ifelse (any? candidates)
    [
      set target-field min-one-of candidates [ distance myself ]
    ]
    [
      set stop-sim-reason "no fields left with adequate rai"
      log2logfile (word "stop simulation: " stop-sim-reason)
      set runsim? false
      stop
    ]
    ask target-field [set is-occupied? true]
    if not (old-target = nobody) [ ask old-target [set is-occupied? false] ]
    if not (target-field = nobody)
    [
      let msg (word "flock " who " chose target field " target-field ". ")
      log2logfile msg
    ]
  ]
end

to graze-here  ;;ai20110815 fgd proc
  let consume (n-ducks * RAI-PER-DUCK)
  set rai (rai - consume)  ;; rai is a patch attribute
 ; expose
end    

;;;;;;;;;;;;;;;;;;
;;Movement Procs;;
;;;;;;;;;;;;;;;;;;

to travel-roads-to [#target]  ;; turtle proc, #target is a patch
   if (#target = nobody)
  [
    set stop-sim-reason "no target"
    set runsim? false
    stop
  ]
  let intA road-access
  let intB [road-access] of #target
  let path-nodes 0  ;;ai: just an initialization
  let path-links 0
  ask intA [
    set path-nodes __network-shortest-path-nodes intB newroads
    set path-links __network-shortest-path-links intB newroads
  ] 
  print (word "short path from " intA " to " intB " is " path-nodes)
  print (word "short path from " intA " to " intB " is " path-links)
  ;;ai: need to confirm that all nodes are connected via roads ...
  if-else (length path-nodes = 0)
  [ 
    print (word "WARNING: no roads from " intA " to " intB)
    move-to intB
    set path-nodes (list intA)
  ]
  [print "traveling roads"]
  move-to intA    ;;ai this moves to the road access of the current patch
  foreach but-first path-nodes
  [
    let next-node ?
    print (word "traveling to " next-node)
    face next-node
    while [distance next-node > 0.5] [forward 0.5]
    move-to next-node
  ]
  move-to #target
  print "reached target"
  print ""
  if infectious? [ ask link-set path-links [ ask mypatches [ set pcontaminated? TRUE ]]]
  road-infect-backyard   
end    

to go-home
  ;print (word who " on way home")
  travel-roads-to home-patch 
end                          

;;;;;;;;;;;;;;;;;;;;;
;; Egg Network Go! ;;
;;;;;;;;;;;;;;;;;;;;;

to pickup-eggs     
  set collecting? TRUE             ;; each day all egg traders assess if it is a pickup day for any of their contact flocks (set-target-eggfarm).
  while [ collecting? ]            ;; if it is, they begin collecting from flocks who have that pickup day.
  [                                
    ifelse all? in-link-neighbors [eggs-gone? ]
    [ 
      set collecting? FALSE        ;; when they are done collecting for the day,
      set-target-market
      move-to target-market
      move-to home-patch                      ;; they go home.
    ]
    [ 
      set-target-eggfarm 
      move-to target-eggfarm
      if influenza?
      [
      if ((contaminated? = TRUE) and (not any? turtles-here with [member? breed poultry-types] with [(infectious? = true) or (latent? = TRUE) or (recovered? = TRUE)]))
        [ if random-float 100 < egg-trader-infect-prob [ ask turtles-here with [member? breed poultry-types] [infect]]]      ;; 20120214 New. added this for egg trader to infect susc farm
      if ((contaminated? = FALSE) and (any? turtles-here with [member? breed poultry-types] with [infectious? = TRUE]))                     
      [ if random-float 100 < egg-trader-contam-prob [ contam-trader] ]                                                 ;; 20120214 New. added this to contaminate egg trader at inf farm
  ] ] ]
end
    
to set-target-eggfarm
  
  ifelse ( day = 1)        ;; those flocks that have their eggs collected 3, 5 or 7 times each week will have a pickup on Mondays (day = 1)                                               
    [ 
      ifelse any? in-link-neighbors with [(eggs-gone? = FALSE) and ((pickup-days = 3) or (pickup-days = 5) or (pickup-days = 7)) ]
      [
        set target-eggfarm min-one-of in-link-neighbors with [ (eggs-gone? = FALSE) and ((pickup-days = 3) or (pickup-days = 5) or (pickup-days = 7)) ] [ distance myself ] 
        print (word "Egg trader " who " chose pickup " ([pxcor] of target-eggfarm) ([pycor] of target-eggfarm))
        ask target-eggfarm [ set eggs-gone? TRUE ]
      ] 
      [ print (word "Egg trader " who " no pickups today")
        set collecting? FALSE 
        set target-eggfarm home-patch ]
    ]
    [ 
      ifelse ( day = 2)   ;; those flocks that have their eggs collected 1, 2, 5 or 7 times each week will have a pickup on Tuesdays (day = 2)                                                  
      [ 
        ifelse any? in-link-neighbors with [(eggs-gone? = FALSE) and ((pickup-days = 1) or (pickup-days = 2) or (pickup-days = 5) or (pickup-days = 7)) ]
        [
          set target-eggfarm min-one-of in-link-neighbors with [(eggs-gone? = FALSE) and ((pickup-days = 1) or (pickup-days = 2) or (pickup-days = 5) or (pickup-days = 7)) ] [ distance myself ] 
         ;print (word "Egg trader " who " chose pickup" ([pxcor] of target-eggfarm) ([pycor] of target-eggfarm))
          ask target-eggfarm [ set eggs-gone? TRUE ]
        ]
        [ ;print (word "Egg trader " who " no pickups today")
          set collecting? FALSE 
          set target-eggfarm home-patch ]
      ]
      [
        ifelse ( day = 3)    ;; those flocks that have their eggs collected 3, 5 or 7 times each week will have a pickup on Wednesdays (day = 3)                                                 
        [ 
          ifelse any? in-link-neighbors with [ (eggs-gone? = FALSE) and ((pickup-days = 3) or (pickup-days = 5) or (pickup-days = 7)) ]
          [
            set target-eggfarm min-one-of in-link-neighbors with [ (eggs-gone? = FALSE) and ((pickup-days = 3) or (pickup-days = 5) or (pickup-days = 7)) ] [ distance myself ] 
            ;print (word "Egg trader " who " chose pickup" ([pxcor] of target-eggfarm) ([pycor] of target-eggfarm))
            ask target-eggfarm [ set eggs-gone? TRUE ]
          ]
          [ ;print (word "Egg trader " who " no pickups today")
            set collecting? FALSE 
            set target-eggfarm home-patch ]
        ]
        [ 
          ifelse ( day = 4)   ;; those flocks that have their eggs collected 2, 5 or 7 times each week will have a pickup on Thursdays (day = 4)
          [
            ifelse any? in-link-neighbors with [(eggs-gone? = FALSE) and ((pickup-days = 2) or (pickup-days = 5) or (pickup-days = 7)) ]                                                 
            [ 
              set target-eggfarm min-one-of in-link-neighbors with [(eggs-gone? = FALSE) and ((pickup-days = 2) or (pickup-days = 5) or (pickup-days = 7)) ] [ distance myself ] 
              ;print (word "Egg trader " who " chose pickup" ([pxcor] of target-eggfarm) ([pycor] of target-eggfarm))
              ask target-eggfarm [ set eggs-gone? TRUE ]
            ]
            [ ;print (word "Egg trader " who " no pickups today")
              set collecting? FALSE 
              set target-eggfarm home-patch ]
          ]
          [ 
            ifelse ( day = 5)    ;; those flocks that have their eggs collected 3, 5 or 7 times each week will have a pickup on Fridays (day = 5)
            [
              ifelse any? in-link-neighbors with [(eggs-gone? = FALSE) and ((pickup-days = 3) or (pickup-days = 5) or (pickup-days = 7)) ]                                              
              [ 
                set target-eggfarm min-one-of in-link-neighbors with [(eggs-gone? = FALSE) and ((pickup-days = 3) or (pickup-days = 5) or (pickup-days = 7)) ] [ distance myself ] 
                ;print (word "Egg trader " who " chose pickup" ([pxcor] of target-eggfarm) ([pycor] of target-eggfarm))
                ask target-eggfarm [ set eggs-gone? TRUE ]
              ]
              [ ;print (word "Egg trader " who " no pickups today")
                set collecting? FALSE 
                set target-eggfarm home-patch ]
            ]
            [ 
              ifelse ( day = 6)    ;; those flocks that have their eggs collected 7 times each week will have a pickup on Saturdays (day = 6)
              [
                ifelse any? in-link-neighbors with [(eggs-gone? = FALSE) and (pickup-days = 7) ]                                                    
                [ 
                  set target-eggfarm min-one-of in-link-neighbors with [(eggs-gone? = FALSE) and (pickup-days = 7) ] [ distance myself ] 
                  ;print (word "Egg trader " who " chose pickup" ([pxcor] of target-eggfarm) ([pycor] of target-eggfarm))
                  ask target-eggfarm [ set eggs-gone? TRUE ]
                ]
                [ ;print (word "Egg trader " who " no pickups today")
                  set collecting? FALSE 
                  set target-eggfarm home-patch ]
              ]
              [ 
                ifelse ( day = 7)   ;; those flocks that have their eggs collected 7 times each week will have a pickup on Sundays (day = 7)
                [
                  ifelse any? in-link-neighbors with [ (eggs-gone? = FALSE) and (pickup-days = 7) ]                                                 
                  [ 
                    set target-eggfarm min-one-of in-link-neighbors with [ (eggs-gone? = FALSE) and (pickup-days = 7) ] [ distance myself ] 
                    ;print (word "Egg trader " who " chose pickup" ([pxcor] of target-eggfarm) ([pycor] of target-eggfarm))
                    ask target-eggfarm [ set eggs-gone? TRUE ]
                  ]
                  [ ;print (word "Egg trader " who " no pickups today")
                    set collecting? FALSE 
                    set target-eggfarm home-patch ]
                ]
                [ 
                  
                  set target-eggfarm home-patch 
                ] ] ] ] ] ] ]
  
end

;; Egg delivery:
to set-target-market            ;; egg traders and flock owners who deliver eggs will go to the market closest to their home
  set target-market min-one-of markets [ distance myself ]    
end                    

to deliver-eggs                 ;; deliver-eggs is code for the applicable flock owners to deliver eggs to market
  if egg-delivery?
  [
    set-target-market
    move-to target-market
    move-to home-patch
  ] 
end


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Live Poultry Trading Network Go! ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Each day of the week, the livetraders are either delivering or not, based on the number of days per week they trade (1, 2 or 3)
;; There is no poultry trading network made at setup, because, unlike the egg trading network, it will change daily based on who they have already collectd birds from
;; On the Go command, each trader that trades on the present day will form a network of the 5 nearest houses that have not yet sold birds during the 2-month iteration


to make-livetrader-pickup-network  ;; This is called by each trader, so it may be called 1, 2 or 3 times each day, depending on the pickup schedule
  type "\ncalled make-livetrader-pickup-network"
  let livebird-suppliers turtles with [ (breed = byduckowners) or (breed = byowners)]
  type ( word "There are " ( count livebird-suppliers with [ livebirds-gone? = FALSE ]) " livebird suppliers with birds." )
  create-livebird-links-to min-n-of 4 livebird-suppliers with [ livebirds-gone? = FALSE ]
  [distance myself]
  ask livebird-links [ set color blue ]
end

to pickup-livebirds     
  set collectingbirds? TRUE             ;; each day all live traders assess if it is a pickup day for any of their contact flocks (set-target-livebirds).
  while [ collectingbirds? ]            ;; if it is, they begin collecting from flocks who have that pickup day.
  [                                
    ifelse all? out-link-neighbors [livebirds-gone? = TRUE ]
    [ 
      set collectingbirds? FALSE        ;; when they are done collecting for the day,
      move-to home-patch                ;; they go home, where they wil slaughter the birds
    ]
    [ 
      set-target-livebirds 
      move-to target-livebirds
      if influenza? 
      [
        if ((contaminated? = TRUE) and (not any? turtles-here with [member? breed poultry-types] with [(infectious? = true) or (latent? = TRUE) or (recovered? = TRUE)]))
        [ if random-float 100 < live-trader-infect-prob [ ask turtles-here with [member? breed poultry-types] [infect]]]    ;; 20120214 New. added this for live trader to infect susc farm
        if ((contaminated? = FALSE) and (any? turtles-here with [member? breed poultry-types] with [infectious? = TRUE]))                     
        [ if random-float 100 < live-trader-contam-prob [ contam-trader] ]                                             ;; 20120214 New. added this to contaminate live trader at inf farm
      ]
    ]
  ]
end

to set-target-livebirds
  set target-livebirds min-one-of out-link-neighbors with [ livebirds-gone? = FALSE ] [ distance myself ] 
  ;print (word "Live bird trader " who " chose pickup" ([pxcor] of target-livebirds) ([pycor] of target-livebirds))
  ask target-livebirds [ set livebirds-gone? TRUE ]
end

to livetrader-pickup
  ifelse ( day = 1)                                    ;; those traders that pick up live birds 3 times each week will pickup on Mondays (day = 1)                                               
    [ 
      ask livetraders with [ bird-pickup-days = 2 ]
      [ 
        make-livetrader-pickup-network                    ;; each livetrader will make a network for the day with the 5 closest flocks with birds-gone= FALSE
        pickup-livebirds
      ]
      ask links with [ color = blue ] [ die ]          ;; clear the links after the trading day, because new network on the next day
    ]
    [ ifelse ( day = 2)                                  ;; those traders that pick up live birds 3 times each week will pickup on Tuesdays (day = 2) 
      [
        ask livetraders with [ bird-pickup-days = 1]
          [ 
            make-livetrader-pickup-network                    
            pickup-livebirds
          ]
        ask links with [ color = blue ] [ die ]          ;; clear the links after the trading day, because new network on the next day
      ]
      [ ifelse ( day = 3)                               ;; those traders that pick up live birds 3 times each week will pickup on Wednesdays (day = 3) 
        [
          ask livetraders with [ bird-pickup-days = 2]
            [ 
              make-livetrader-pickup-network                    
              pickup-livebirds
            ]
          ask links with [ color = blue ] [ die ]          ;; clear the links after the trading day, because new network on the next day
        ]
        [ ifelse ( day = 4)                                ;; those traders that pick up live birds 2 times each week will pickup on Thursdays (day = 4) 
          [
            ask livetraders with [ bird-pickup-days = 1]
              [ 
                make-livetrader-pickup-network                    
                pickup-livebirds
              ]
            ask links with [ color = blue ] [ die ]          ;; clear the links after the trading day, because new network on the next day
          ]
          [ ifelse ( day = 5)                                ;; those traders that pick up live birds 1 or 3 times each week will pickup on Fridays (day = 5) 
            [
              ask livetraders with [ (bird-pickup-days = 2) or (bird-pickup-days = 0) ]
                [ 
                  make-livetrader-pickup-network                    
                  pickup-livebirds
                ]
              ask links with [ color = blue ] [ die ]          ;; clear the links after the trading day, because new network on the next day
            ]
            [ print ( word "No live bird trade on weekend.") ] 
          ]]]]
  
end


;;;;;;;;;;;;;;;;;;;;;;;
;;Fighting Cock Procs;;
;;;;;;;;;;;;;;;;;;;;;;;

to fight-cocks
  if ((day != 7) and (day != 3)) [ type (word "No fighting cock activity today. ")]
  if (day = 3 )
  [ 
    ask fc-owners [move-to target-practice-arena]
    ;ai20120109: changed to arenas printing this info (more efficient)
    ask practice-arena-1 [type (word "There are " count byowners-here " fc owners at arena 1.")]  
    ask practice-arena-2 [type (word "There are " count byowners-here " fc owners at arena 2.")]  
    type "FCs practicing are " print ([who] of fc-owners with [practice-fight-week?])
    ask fc-owners                                   ;; one arena always seems to have more cocks than the other. Should a limit of 25 each be implemented??
    [ if practice-fight-week? [fight-here ]] 
    ask fc-owners 
    [ 
      set practice-fight-week? (not practice-fight-week?)
    ]
    ask fc-owners [ move-to home-patch infect-backyard]
  ]
  if (day = 7)
    [ 
      ask fc-owners [ if random-float 100 < 3.8 [ set match-fight-week? TRUE]]      ;; based on each fc-owner bringing a FC to the match arena twice yearly
      ask fc-owners 
      [ 
        if (match-fight-week? = TRUE)
        [
          move-to target-match-arena
          fight-here 
          print (word "FC " who " is fighting")
        ]
      ]
      ask fc-owners [ move-to home-patch infect-backyard]                                                ;; but, this makes it so that only 2-3 FC from this subdistrict 
    ]                                                                                                    ;; are fighting each week, so may need to incorporate some risk 
end                                                                                                      ;; posed by birds from other subdistricts?

to fight-here
  if influenza?
    [
    if ((any? turtles-here with [(breed = fc-owners) and (infectious? = true)]) or (infected-outsider-FC-here? = TRUE))   ;; 20120214 New.
    [
      if random-float 100 < FC-infect-prob                              ;; if there are infectious flighting cocks at the arena, 
      [                                                                 ;; infect the fighting cock in question with a prob of established at setup
        set latent? TRUE                                                ;; FC-owners are the only human-types that have a state transition.  
        set color 117                                                   ;; starts as latent, color yellow and day 0 is set
        set infection-time 0
        set own-infected-fc? TRUE
      ]
    ]
  ]
end

;;;;;;;;;;;;;;;;;;
;;Backyard Procs;;
;;;;;;;;;;;;;;;;;;

to visit ;; byowner procedure
         ;;ai20110829 I'm going to make some changes here; see my email
         ;; all backyard owners can move to a few neighbors daily for a visit
         ;; number of visits per day indicated by binomial above
  ask byowners
  [
    foreach [self] of visit-list
    [
      let msg (word "Bkyd " who " at " patch-here " visiting " ?)
      ;print msg
      log2logfile msg
      move-to ?
      if (own-infected-flock? = FALSE and (any? turtles-here with [infectious? = TRUE]))  
      [ set contaminated? TRUE ]                   
    ]
    move-to home-patch
    let msg ( word "Bkyd " who " heading home to " patch-here ".\n" )
    ;print msg
    log2logfile msg
    if contaminated? = TRUE                                                          ;; 20120214 New. added this to contaminate backyard owner at inf farm
    [ if random-float 100 < visit-infect-prob [ ask turtles-here with [member? breed poultry-types] [infect] ] ]
    set contaminated? FALSE                          
  ]
  ask fc-owners
  [
    foreach [self] of visit-list
    [
      let msg (word "FC-owner " who " at " patch-here " visiting " ?)
      ;print msg
      log2logfile msg
      move-to ?
      if (own-infected-flock? = FALSE and (any? turtles-here with [infectious? = TRUE]))  
      [ set contaminated? TRUE ]                   
    ]
    move-to home-patch
    let msg ( word "FC-owner " who " heading home to " patch-here ".\n" )
    ;print msg
    log2logfile msg
    if contaminated? = TRUE                                                          ;; 20120214 New. added this to contaminate backyard owner at inf farm
    [ if random-float 100 < visit-infect-prob [ ask turtles-here with [member? breed poultry-types] [infect] ] ]
    set contaminated? FALSE                          
  ]
  ask fgdowners
  [
    foreach [self] of visit-list
    [
      let msg (word "FGDowner " who " at " patch-here " visiting " ?)
     ; print msg
      log2logfile msg
      move-to ?
      if (own-infected-flock? = FALSE and (any? turtles-here with [infectious? = TRUE]))  
      [ set contaminated? TRUE ]  
    ]
    move-to home-patch
    let msg ( word "FGDowner " who " heading home to " patch-here ".\n" )
    ;print msg
    log2logfile msg
    if contaminated? = TRUE
    [ if random-float 100 < visit-infect-prob [ ask turtles-here with [member? breed poultry-types] [infect] ] ]
    set contaminated? FALSE
  ]
  ask byduckowners
  [
    foreach [self] of visit-list
    [
      let msg (word "Bkyduck " who " at " patch-here " visiting " ?)
     ; print msg
      log2logfile msg
      move-to ?
      if (own-infected-flock? = FALSE and (any? turtles-here with [infectious? = TRUE]))  
      [ set contaminated? TRUE ]  
    ]
    move-to home-patch
    let msg ( word "Bkyduck " who " heading home to " patch-here ".\n" )
    ;print msg
    log2logfile msg
    if contaminated? = TRUE
    [ if random-float 100 < visit-infect-prob [ ask turtles-here with [member? breed poultry-types] [infect] ] ]
    set contaminated? FALSE
  ]
  ask farmowners
  [
    foreach [self] of visit-list
    [
      let msg (word "Farmowner " who " at " patch-here " visiting " ?)
     ; print msg
      log2logfile msg
      move-to ?
      if (own-infected-flock? = FALSE and (any? turtles-here with [infectious? = TRUE]))  
      [ set contaminated? TRUE ]  
    ]
    move-to home-patch
    let msg ( word "Farmowner " who " heading home to " patch-here ".\n" )
    ;print msg
    log2logfile msg
    if contaminated? = TRUE
    [ if random-float 100 < visit-infect-prob [ ask turtles-here with [member? breed poultry-types] [infect] ] ]
    set contaminated? FALSE
  ]
end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Contamination and Infection;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to infect-fgd               ;; infect an fgd at model setup
   if influenza?
  [
  set infectious? TRUE
  set color pink 
  set shedding-time 0
  ]
end

to road-infect-backyard
  if influenza?
  [
    if pcontaminated? = TRUE
    [
      ask bychickens
      [ if random-float 100 < road-infect-prob     ;; right now this is defined as the most likely value, as defined in setup
        [ infect ]
      ]
    ]
  ]
end
    
to infect
  if influenza?
  [
  if (latent? = FALSE) and (infectious? = FALSE) and (recovered? = FALSE)
  [
  set latent? TRUE
  set infection-time 0
  print (word "flock " who " is infected")
  ]
  ]
end

to contam-trader
  set contaminated? TRUE
  set trader-contam-time 0
  print (word "trader " who " is contaminated")
end

to infect-backyard                       ;; in fighting procedures, ask fc-owners to infect-backyard poultry
  if influenza?                          ;; assumption is that is flock is not infected by the FC, then the infection dies and is not transported to neighbors
  [
    if ((own-infected-fc? = TRUE) and (not any? turtles-here with [member? breed poultry-types] with [(infectious? = true) or (latent? = TRUE) or (recovered? = TRUE)]))
    [ if random-float 100 < FC-bkyd-infect-prob [ ask turtles-here with [member? breed poultry-types] [infect]]]    ;; 20120214 New.
  ]
end

to contaminate-field ;;ai20110815 patch proc             ;; This is where we will be able to give a continuous contamination value to the rice fields
  set field-contaminant (field-infection-prob / 100)     ;; This will be used if we want to generate an environmental SIR transition model for virus survival in fields
  set plabel "c"                                         ;; ai20110815 provisional (just matches what you have been doing, but perhaps shd change
  set plabel-color blue - 2                              ;; set label of contaminiated field
end


;;;;;;;;;;;;;;;;;;;;;;
;;Transmission Procs;;
;;;;;;;;;;;;;;;;;;;;;;

to change-state                                          ;; both ducks and chickens have a 1 day latent period. 
  if latent?                                             
    [
      if infection-time > latent-period
      [ 
        set infectious? TRUE
        set latent? FALSE
        set color pink
        set shedding-time 0
      ]
    ] 
  if (breed = bychickens) or ((breed = farms) and (egg-type = "chicken")) or (breed = fc-owners ) 
  [
    if infectious?
    [
      if shedding-time > shedding-period-chicken          ;; chickens have a 4 day shedding period. 
      [ 
        set recovered? TRUE
        set infectious? FALSE
        set color gray
      ]
    ]
  ]
  if (breed = bychickens) or ((breed = farms) and (egg-type = "chicken")) or (breed = byducks) or (breed = fgds)
  [
    if infectious?
    [
      if shedding-time > shedding-period-duck             ;; ducks have a 7 day shedding period. 
      [ 
        set recovered? TRUE
        set infectious? FALSE
        set color gray
      ]
    ]
  ]
end

to add-influenza-day                                                        ;; how to keep time moving forward
      if latent?
      [ set infection-time infection-time + 1 ]
      if infectious? 
      [ set shedding-time shedding-time + 1 ]
      set eggs-gone? FALSE
end

to inform-owner
  ask turtles-here with [member? breed human-types]
  [ set own-infected-flock? TRUE]
  ask byowners-here [ set infectious? TRUE]
end

to set-road-contam-time
    if pcontaminated? = TRUE [ set road-contam-time road-contam-time + 1 ]
end

;to all-add-influenza-day
;  ask fgds [ add-influenza-day ]
;  ask bychickens [ add-influenza-day ]
;  ask farms [ add-influenza-day ]
;  ask byducks [ add-influenza-day ]
;end

;to all-change-state
;    ask fgds [ change-state ]
;    ask bychickens [ change-state ]
;    ask byducks [ change-state ]
;    ask farms [ change-state ]
;end


to decontaminate-traders
    if trader-contam-time >= trader-contam-period
      [
        set contaminated? FALSE
        set trader-contam-time 0
      ]
end

to decontaminate-roads
    if road-contam-time >= road-contam-period
      [
        set pcontaminated? FALSE
        set road-contam-time 0
      ]
end

to set-day
  set day day + 1                                     ;; after each tick, one day passes
  if ( day = 8 )                                      ;; reset the week after 7 days
    [
      set day 1
      print ( word "The week is over")
    ]
  if ( day = 1 )                                      
    [ print ( word "Monday is starting" day ) ]
  if ( day = 2 )                                      
    [ print ( word "Tuesday is starting") ]
  if ( day = 3 )                                      
    [ print ( word "Wednesday is starting") ]
  if ( day = 4 )                                      
    [ print ( word "Thursday is starting") ]
  if ( day = 5 )                                      
    [ print ( word "Friday is starting") ]
  if ( day = 6 )                                      
    [ print ( word "Saturday is starting") ]
  if ( day = 7 )                                      
    [ print ( word "Sunday is starting") ]
  if (ticks = 61)
    [
    set stop-sim-reason (word "Reached max iter of " ticks " ticks.")
    set runsim? false]                                  ;; set the limit for the number of tick to be 61 days, or 2 months, 
                                                        ;; which is the approximate time of the harvest period.
end


to go
  ifelse runsim?
  [
    type (word "ticks is " ticks ". ")
    ask eggtraders [ pickup-eggs ]                     ;; moved egg trading to first so as to facilitate the transmission process, becuase
    go-to-field                                        ;; the contam of the egg trader depends on there being infectious turtles, not infected patch.   
    ask fgdowners [ deliver-eggs ]
    ask farmowners [deliver-eggs ]
    ask byduckowners [deliver-eggs ]
    livetrader-pickup
    fight-cocks
    ask fgds with [field-only? = FALSE] [ go-home ]
    visit 
    do-plots
    if influenza?
    [ ask barns [set-road-contam-time] 
      ask turtles with [member? breed poultry-types] [add-influenza-day change-state ] ]
    ask fc-owners [change-state]
    ask turtles with [member? breed trader-types] [decontaminate-traders]
    set-day
    tick
  ]
  [
    print (word "Simulation stopped: " stop-sim-reason)
    stop
  ]
end

to do-plots                                                                ;; not yet updated for the state changes
  set-current-plot "Contamination"
  set-current-plot-pen "Barns Near Road"
  plot count barns with [pcontaminated? = TRUE]
  set-current-plot-pen "Rice Fields"
  plot count rice-fields with [pcontaminated? = TRUE]
  set-current-plot-pen "Egg Traders"
  plot count eggtraders with [contaminated? = TRUE]
  set-current-plot-pen "Live Traders"
  plot count livetraders with [contaminated? = TRUE]
  
  set-current-plot "FGD Infection State"
  set-current-plot-pen "Latent Flocks"
  plot count fgds with [ latent? ]
  set-current-plot-pen "Infectious Flocks"
  plot count fgds with [ infectious? ]
  set-current-plot-pen "Recovered Flocks"
  plot count fgds with [ recovered? ]
  
  set-current-plot "Backyard Chickens Infection State"
  set-current-plot-pen "Latent Flocks"
  plot count bychickens with [ latent? ]
  set-current-plot-pen "Infectious Flocks"
  plot count bychickens with [ infectious? ]
  set-current-plot-pen "Recovered Flocks"
  plot count bychickens with [ recovered? ]
  
end



@#$#@#$#@
GRAPHICS-WINDOW
325
10
1022
728
30
30
11.2623
1
10
1
1
1
0
0
0
1
-30
30
-30
30
0
0
1
days

BUTTON
20
10
84
43
NIL
Setup
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL

BUTTON
115
10
178
43
NIL
Go
T
1
T
OBSERVER
NIL
NIL
NIL
NIL

SLIDER
20
50
208
83
num-fgd-barntofield
num-fgd-barntofield
0
10
6
1
1
flocks
HORIZONTAL

SLIDER
19
134
191
167
num-fgd-inf
num-fgd-inf
0
5
1
1
1
flocks
HORIZONTAL

PLOT
20
255
262
405
Contamination
time
totals
0.0
10.0
0.0
10.0
true
true
PENS
"Barns Near Road" 1.0 0 -13345367 true
"Rice Fields" 1.0 0 -13840069 true
"Egg Traders" 1.0 0 -955883 true
"Live Traders" 1.0 0 -5825686 true

PLOT
19
413
260
563
FGD Infection State
time
totals
0.0
10.0
0.0
10.0
true
true
PENS
"Latent Flocks" 1.0 0 -10899396 true
"Infectious Flocks" 1.0 0 -2674135 true
"Recovered Flocks" 1.0 0 -5987164 true

SLIDER
19
216
191
249
num-fields-contam
num-fields-contam
0
10
0
1
1
NIL
HORIZONTAL

SLIDER
19
175
191
208
num-fields-harvested
num-fields-harvested
0
40
32
1
1
NIL
HORIZONTAL

MONITOR
1032
25
1177
70
non-rice patches
patches with [field-state = \"\" ]
0
1
11

MONITOR
1034
76
1175
121
rice fields
rice-fields
0
1
11

SWITCH
195
10
308
43
Influenza?
Influenza?
0
1
-1000

MONITOR
1037
132
1158
177
chicken egg traders
count eggtraders with [ egg-type = \"chicken\"]
0
1
11

MONITOR
1037
186
1143
231
duck egg traders
count eggtraders with [egg-type = \"duck\"]
0
1
11

MONITOR
1037
238
1211
283
chicken and duck egg traders
count eggtraders with [egg-type = \"duck and chicken\"]
0
1
11

MONITOR
1038
295
1176
340
number of egg traders
count eggtraders
0
1
11

MONITOR
1039
352
1221
397
number of fighters this week
count byowners with [practice-fight-week? = true]
0
1
11

SLIDER
21
91
193
124
num-fgd-fieldonly
num-fgd-fieldonly
0
10
3
1
1
NIL
HORIZONTAL

PLOT
20
575
260
725
Backyard Chickens Infection State
time
totals
0.0
10.0
0.0
10.0
true
true
PENS
"Latent Flocks" 1.0 0 -10899396 true
"Infectious Flocks" 1.0 0 -2674135 true
"Recovered Flocks" 1.0 0 -5987164 true

@#$#@#$#@
WHAT IS IT?
-----------
This is an agent-based model of the subdistrict poultry sector in Central Thailand.

For this project, we conducted interviews with free-grazing duck owners, backyard poultry owners, large farm owners, poultry traders, slaughterhouse workers and egg traders in three subdistricts of Suphanburi Province, Thailand. The interviews focused on the frequency and character of poultry-related contacts, including the transport, sale and purchase of both birds and eggs. All of these aspects are reflected in the model. The agents interact on a spatially representative backdrop of a Thai subdistrict. 

The objective for modeling this system is to observe the contact frequencies and types that occur among members of the subdistrict poultry sector and hypothesize what contact types pose the greatest risk for transmission of highly pathogenic avian influenza (HPAI) H5N1. Generation of different scenarios also allow the assessment of interventions that could result in a decrease of between-flock avian influenza virus transmission risk. 


HOW IT WORKS
------------
BROWN-Roads
GREEN- Fields with growing rice
YELLOW- Fields with harvested rice
LIGHT YELLOW- Fallow fields
BLUE- Barns
RED- Fighting cock arenas
VIOLET- Markets or egg cooperative (marked with "C")
PINK- Slaughterhouses

GOLD PERSON- Backyard chicken owner, subset owns fighting cocks
Backyard characteristics: Freely roam around home yard and close to/ into road. Do not travel from home. No eggs collected/ delivered. Infrequent pickup by live poultry traders.
Backyard contacts made: FGD passing by yard on foot (direct); Infected fighting cocks returning to flock (direct); FGD by contaminated roads (indirect); Other flocks by live poultry traders (indirect); Other flocks by owner visiting (indirect)

Fighting cock characteristics: Owners go weekly to practice arena for fights, but do not bring cocks every time. Owners bring fighting cocks at rate of twice yearly to match arena.
Fighting cock contacts made: Fighting against infectious cock at practice or match (direct); Infectious birds at practice arena, owner as fomite (indirect)

WHITE DUCK- FGD flock; WHITE PERSON- FGD owner
Characteristics: Move from home to field daily. Have the potential to become infected at the rice field via theoretical interaction with wild birds. When traveling on the roads from home to field may contact, directly or indirectly, backyard poultry. Have eggs collected/ delivered.
Contacts made: Backyard poultry en route to fields (direct); Backyard poultry by contaminated roads (indirect); Other flocks by egg collectors (indirect); Other flocks by owner visiting (indirect)

GREEN PERSON- Chicken or duck farm owner
Characteristics: Large flocks of chickens or ducks kept in barns, usually with open sides and located over body of water. Used primarily for egg production.
Contacts made: Other flocks by egg collectors (indirect); Other flocks by owner visiting (indirect)

BROWN PERSON- Backyard duck owner
Characteristics: Penned ducks in small flocks of approx. 50-500 head. Do not travel from home. Eggs picked up/ delivered. Infrequent pickup by live poultry traders.
Contacts made: FGD by contaminated roads?(indirect); Other flocks by egg collectors (indirect); Other flocks by live poultry traders (indirect); Other flocks by owner visiting

CAR- Egg trader
Characteristics: Create a network of farms to go to on a certain number of trading days each week. Go to the same farms on each trading day. Bring eggs to a specific market or egg cooperative each trading day.
Contacts made: Farms where pick up eggs (contamination source); Flocks by acting as fomites, via shoes and vehicles (location to contaminate)

TRUCK- Live poultry trader
Characteristics: On a certain number of trading days each week, create a network of farms to go to. Go to the different farms on each trading day. Bring birds back to home for slaughter.
COntacts made: Farms where pick up birds (contamination source); Flocks by acting as fomites, via shoes, clothing and vehicles (location to contaminate)


HOW TO USE IT
-------------
This section could explain how to use the model, including a description of each of the items in the interface tab.


THINGS TO NOTICE
----------------
This section could give some ideas of things for the user to notice while running the model.


THINGS TO TRY
-------------
This section could give some ideas of things for the user to try to do (move sliders, switches, etc.) with the model.


EXTENDING THE MODEL
-------------------
This section could give some ideas of things to add or change in the procedures tab to make the model more complicated, detailed, accurate, etc.


NETLOGO FEATURES
----------------
This section could point out any especially interesting or unusual features of NetLogo that the model makes use of, particularly in the Procedures tab.  It might also point out places where workarounds were needed because of missing features.


RELATED MODELS
--------------
This section could give the names of models in the NetLogo Models Library or elsewhere which are of related interest.


CREDITS AND REFERENCES
----------------------
This section could contain a reference to the model's URL on the web if it has one, as well as any other necessary credits or references.
@#$#@#$#@
default
true
0
Polygon -7500403 true true 150 5 40 250 150 205 260 250

airplane
true
0
Polygon -7500403 true true 150 0 135 15 120 60 120 105 15 165 15 195 120 180 135 240 105 270 120 285 150 270 180 285 210 270 165 240 180 180 285 195 285 165 180 105 180 60 165 15

arrow
true
0
Polygon -7500403 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150

backyard
true
0
Circle -955883 true false 73 101 64
Rectangle -955883 true false 105 137 165 240
Rectangle -2674135 true false 84 91 127 112
Circle -1184463 true false 92 205 38
Circle -1184463 true false 138 205 40
Circle -11221820 true false 91 116 14
Polygon -1184463 true false 64 120 45 135 75 150 75 120

bird side
false
0
Polygon -7500403 true true 0 120 45 90 75 90 105 120 150 120 240 135 285 120 285 135 300 150 240 150 195 165 255 195 210 195 150 210 90 195 60 180 45 135
Circle -16777216 true false 38 98 14

box
false
0
Polygon -7500403 true true 150 285 285 225 285 75 150 135
Polygon -7500403 true true 150 135 15 75 150 15 285 75
Polygon -7500403 true true 15 75 15 225 150 285 150 135
Line -16777216 false 150 285 150 135
Line -16777216 false 150 135 15 75
Line -16777216 false 150 135 285 75

bug
true
0
Circle -7500403 true true 96 182 108
Circle -7500403 true true 110 127 80
Circle -7500403 true true 110 75 80
Line -7500403 true 150 100 80 30
Line -7500403 true 150 100 220 30

butterfly
true
0
Polygon -7500403 true true 150 165 209 199 225 225 225 255 195 270 165 255 150 240
Polygon -7500403 true true 150 165 89 198 75 225 75 255 105 270 135 255 150 240
Polygon -7500403 true true 139 148 100 105 55 90 25 90 10 105 10 135 25 180 40 195 85 194 139 163
Polygon -7500403 true true 162 150 200 105 245 90 275 90 290 105 290 135 275 180 260 195 215 195 162 165
Polygon -16777216 true false 150 255 135 225 120 150 135 120 150 105 165 120 180 150 165 225
Circle -16777216 true false 135 90 30
Line -16777216 false 150 105 195 60
Line -16777216 false 150 105 105 60

car
false
0
Polygon -7500403 true true 300 180 279 164 261 144 240 135 226 132 213 106 203 84 185 63 159 50 135 50 75 60 0 150 0 165 0 225 300 225 300 180
Circle -16777216 true false 180 180 90
Circle -16777216 true false 30 180 90
Polygon -16777216 true false 162 80 132 78 134 135 209 135 194 105 189 96 180 89
Circle -7500403 true true 47 195 58
Circle -7500403 true true 195 195 58

circle
false
0
Circle -7500403 true true 0 0 300

circle 2
false
0
Circle -7500403 true true 0 0 300
Circle -16777216 true false 30 30 240

cow
false
0
Polygon -7500403 true true 200 193 197 249 179 249 177 196 166 187 140 189 93 191 78 179 72 211 49 209 48 181 37 149 25 120 25 89 45 72 103 84 179 75 198 76 252 64 272 81 293 103 285 121 255 121 242 118 224 167
Polygon -7500403 true true 73 210 86 251 62 249 48 208
Polygon -7500403 true true 25 114 16 195 9 204 23 213 25 200 39 123

cylinder
false
0
Circle -7500403 true true 0 0 300

dot
false
0
Circle -7500403 true true 90 90 120

duck
true
3
Circle -6459832 true true 150 45 60
Circle -6459832 true true 74 74 122
Rectangle -955883 true false 150 45 165 60
Line -955883 false 75 150 60 150
Line -955883 false 75 120 60 120
Line -955883 false 90 105 60 105
Line -955883 false 90 165 60 165
Line -955883 false 60 105 60 120
Line -955883 false 60 150 60 165
Rectangle -955883 true false 60 105 75 120
Rectangle -955883 true false 75 105 90 120
Rectangle -955883 true false 60 150 90 165
Rectangle -11221820 true false 180 60 195 75

face happy
false
0
Circle -7500403 true true 8 8 285
Circle -16777216 true false 60 75 60
Circle -16777216 true false 180 75 60
Polygon -16777216 true false 150 255 90 239 62 213 47 191 67 179 90 203 109 218 150 225 192 218 210 203 227 181 251 194 236 217 212 240

face neutral
false
0
Circle -7500403 true true 8 7 285
Circle -16777216 true false 60 75 60
Circle -16777216 true false 180 75 60
Rectangle -16777216 true false 60 195 240 225

face sad
false
0
Circle -7500403 true true 8 8 285
Circle -16777216 true false 60 75 60
Circle -16777216 true false 180 75 60
Polygon -16777216 true false 150 168 90 184 62 210 47 232 67 244 90 220 109 205 150 198 192 205 210 220 227 242 251 229 236 206 212 183

farm
true
0
Circle -1184463 true false 73 101 64
Rectangle -1184463 true false 105 137 165 240
Rectangle -2674135 true false 84 91 127 112
Circle -955883 true false 92 205 38
Circle -955883 true false 138 205 40
Circle -11221820 true false 91 116 14
Polygon -955883 true false 64 120 45 135 75 150 75 120

fish
false
0
Polygon -1 true false 44 131 21 87 15 86 0 120 15 150 0 180 13 214 20 212 45 166
Polygon -1 true false 135 195 119 235 95 218 76 210 46 204 60 165
Polygon -1 true false 75 45 83 77 71 103 86 114 166 78 135 60
Polygon -7500403 true true 30 136 151 77 226 81 280 119 292 146 292 160 287 170 270 195 195 210 151 212 30 166
Circle -16777216 true false 215 106 30

flag
false
0
Rectangle -7500403 true true 60 15 75 300
Polygon -7500403 true true 90 150 270 90 90 30
Line -7500403 true 75 135 90 135
Line -7500403 true 75 45 90 45

flower
false
0
Polygon -10899396 true false 135 120 165 165 180 210 180 240 150 300 165 300 195 240 195 195 165 135
Circle -7500403 true true 85 132 38
Circle -7500403 true true 130 147 38
Circle -7500403 true true 192 85 38
Circle -7500403 true true 85 40 38
Circle -7500403 true true 177 40 38
Circle -7500403 true true 177 132 38
Circle -7500403 true true 70 85 38
Circle -7500403 true true 130 25 38
Circle -7500403 true true 96 51 108
Circle -16777216 true false 113 68 74
Polygon -10899396 true false 189 233 219 188 249 173 279 188 234 218
Polygon -10899396 true false 180 255 150 210 105 210 75 240 135 240

house
false
0
Rectangle -7500403 true true 45 120 255 285
Rectangle -16777216 true false 120 210 180 285
Polygon -7500403 true true 15 120 150 15 285 120
Line -16777216 false 30 120 270 120

leaf
false
0
Polygon -7500403 true true 150 210 135 195 120 210 60 210 30 195 60 180 60 165 15 135 30 120 15 105 40 104 45 90 60 90 90 105 105 120 120 120 105 60 120 60 135 30 150 15 165 30 180 60 195 60 180 120 195 120 210 105 240 90 255 90 263 104 285 105 270 120 285 135 240 165 240 180 270 195 240 210 180 210 165 195
Polygon -7500403 true true 135 195 135 240 120 255 105 255 105 285 135 285 165 240 165 195

line
true
0
Line -7500403 true 150 0 150 300

line half
true
0
Line -7500403 true 150 0 150 150

pentagon
false
0
Polygon -7500403 true true 150 15 15 120 60 285 240 285 285 120

person
false
0
Circle -7500403 true true 110 5 80
Polygon -7500403 true true 105 90 120 195 90 285 105 300 135 300 150 225 165 300 195 300 210 285 180 195 195 90
Rectangle -7500403 true true 127 79 172 94
Polygon -7500403 true true 195 90 240 150 225 180 165 105
Polygon -7500403 true true 105 90 60 150 75 180 135 105

plant
false
0
Rectangle -7500403 true true 135 90 165 300
Polygon -7500403 true true 135 255 90 210 45 195 75 255 135 285
Polygon -7500403 true true 165 255 210 210 255 195 225 255 165 285
Polygon -7500403 true true 135 180 90 135 45 120 75 180 135 210
Polygon -7500403 true true 165 180 165 210 225 180 255 120 210 135
Polygon -7500403 true true 135 105 90 60 45 45 75 105 135 135
Polygon -7500403 true true 165 105 165 135 225 105 255 45 210 60
Polygon -7500403 true true 135 90 120 45 150 15 180 45 165 90

sheep
false
0
Rectangle -7500403 true true 151 225 180 285
Rectangle -7500403 true true 47 225 75 285
Rectangle -7500403 true true 15 75 210 225
Circle -7500403 true true 135 75 150
Circle -16777216 true false 165 76 116

square
false
0
Rectangle -7500403 true true 30 30 270 270

square 2
false
0
Rectangle -7500403 true true 30 30 270 270
Rectangle -16777216 true false 60 60 240 240

star
false
0
Polygon -7500403 true true 151 1 185 108 298 108 207 175 242 282 151 216 59 282 94 175 3 108 116 108

target
false
0
Circle -7500403 true true 0 0 300
Circle -16777216 true false 30 30 240
Circle -7500403 true true 60 60 180
Circle -16777216 true false 90 90 120
Circle -7500403 true true 120 120 60

tree
false
0
Circle -7500403 true true 118 3 94
Rectangle -6459832 true false 120 195 180 300
Circle -7500403 true true 65 21 108
Circle -7500403 true true 116 41 127
Circle -7500403 true true 45 90 120
Circle -7500403 true true 104 74 152

triangle
false
0
Polygon -7500403 true true 150 30 15 255 285 255

triangle 2
false
0
Polygon -7500403 true true 150 30 15 255 285 255
Polygon -16777216 true false 151 99 225 223 75 224

truck
false
0
Rectangle -7500403 true true 4 45 195 187
Polygon -7500403 true true 296 193 296 150 259 134 244 104 208 104 207 194
Rectangle -1 true false 195 60 195 105
Polygon -16777216 true false 238 112 252 141 219 141 218 112
Circle -16777216 true false 234 174 42
Rectangle -7500403 true true 181 185 214 194
Circle -16777216 true false 144 174 42
Circle -16777216 true false 24 174 42
Circle -7500403 false true 24 174 42
Circle -7500403 false true 144 174 42
Circle -7500403 false true 234 174 42

turtle
true
0
Polygon -10899396 true false 215 204 240 233 246 254 228 266 215 252 193 210
Polygon -10899396 true false 195 90 225 75 245 75 260 89 269 108 261 124 240 105 225 105 210 105
Polygon -10899396 true false 105 90 75 75 55 75 40 89 31 108 39 124 60 105 75 105 90 105
Polygon -10899396 true false 132 85 134 64 107 51 108 17 150 2 192 18 192 52 169 65 172 87
Polygon -10899396 true false 85 204 60 233 54 254 72 266 85 252 107 210
Polygon -7500403 true true 119 75 179 75 209 101 224 135 220 225 175 261 128 261 81 224 74 135 88 99

wheel
false
0
Circle -7500403 true true 3 3 294
Circle -16777216 true false 30 30 240
Line -7500403 true 150 285 150 15
Line -7500403 true 15 150 285 150
Circle -7500403 true true 120 120 60
Line -7500403 true 216 40 79 269
Line -7500403 true 40 84 269 221
Line -7500403 true 40 216 269 79
Line -7500403 true 84 40 221 269

x
false
0
Polygon -7500403 true true 270 75 225 30 30 225 75 270
Polygon -7500403 true true 30 75 75 30 270 225 225 270

@#$#@#$#@
NetLogo 4.1.2
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
default
0.0
-0.2 0 1.0 0.0
0.0 1 1.0 0.0
0.2 0 1.0 0.0
link direction
true
0
Line -7500403 true 150 150 90 180
Line -7500403 true 150 150 210 180

@#$#@#$#@
1
@#$#@#$#@
